package org.ycx.upms.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.ycx.upms.client.impl.RedisSecurityMetaDataImpl;

/**
 * Created by 杨 on 2017-06-26.
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    UserDetailsService customUserService() { //注册UserDetailsService 的bean
        return new CustomUserDetailService ();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService (customUserService ()); //sec Details Service验证

    }


    @Autowired
    private CustomFilterInvocationSecurityMetadataSource metadataSource;


    @Autowired
    private CustomerAccessDecisionManager decisionManager;

    @Bean
    public UpmsSecurityInterceptor filterSecurityInterceptor() throws Exception {
        UpmsSecurityInterceptor fi = new UpmsSecurityInterceptor ();
        fi.setSecurityMetadataSource (metadataSource);
        fi.setAccessDecisionManager (decisionManager);
        fi.setAuthenticationManager (authenticationManagerBean ());
        return fi;
    }
    @Bean
    ISecurityMetaData redisSecurityMetaData() {
        return new RedisSecurityMetaDataImpl ();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests ()
                .antMatchers ( "/js/**", "/css/**", "/images/**", "/plugins/**", "/**/favicon.ico").permitAll ()
                .anyRequest ().authenticated ()
                .and ().formLogin ().loginPage ("/login.html").loginProcessingUrl ("/login").defaultSuccessUrl ("/index.html").permitAll ()
                .and ().logout ().logoutRequestMatcher (new AntPathRequestMatcher ("/logout")).logoutSuccessUrl ("/login.html")
                .permitAll ();
        http.addFilterBefore (filterSecurityInterceptor (), FilterSecurityInterceptor.class);
        http.headers ().frameOptions ().sameOrigin ();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring ().antMatchers ("/js/**", "/css/**", "/images/**", "/plugins/**", "/**/favicon.ico");
    }
}
